import requests
from typing import Dict
import config


class StyleEnhancer:
    def __init__(self):
        self.api_key = config.DEEPSEEK_API_KEY
        self.api_url = "https://api.deepseek.com/v1/chat/completions"
        self.headers = {
            "Authorization": f"Bearer {self.api_key}",
            "Content-Type": "application/json"
        }

    def enhance(self, content: Dict, style_description: str) -> Dict:
        """Enhance poster content based on the specified style."""

        # Create a prompt for style enhancement
        prompt = f"""
        Enhance the following academic poster content to match this style:
        "{style_description}"

        Original poster content:
        {content}

        Please provide:
        1. Enhanced title presentation
        2. Color scheme recommendation (specific hex codes)
        3. Typography suggestions (font families, sizes)
        4. Layout recommendations
        5. Visual element styling (borders, shadows, etc.)
        6. Background design suggestions
        7. Enhanced wording for key sections to match the style

        Return the enhanced content and style guidance in a structured JSON format 
        that preserves all the original information but enhances it.
        """

        # Call DeepSeek API for style enhancement
        response = requests.post(
            self.api_url,
            headers=self.headers,
            json={
                "model": config.LLM_MODEL,
                "messages": [
                    {"role": "system",
                     "content": "You are an expert in academic poster design with a strong sense of aesthetics."},
                    {"role": "user", "content": prompt}
                ],
                "temperature": 0.7,
                "max_tokens": 2000
            }
        )

        if response.status_code == 200:
            enhanced_content = response.json()["choices"][0]["message"]["content"]

            # Attempt to parse as JSON, fall back to original content with style additions if parsing fails
            try:
                import json
                enhanced_data = json.loads(enhanced_content)
                return enhanced_data
            except:
                # If JSON parsing fails, add style guidance to original content
                content["style_guidance"] = enhanced_content
                return content
        else:
            # If API call fails, return original content
            content["style_guidance"] = f"Failed to enhance style: {response.status_code}"
            return content